<?php
    /**
    * Pastor save
    * For Prayonline
    * 
    * @author: trungdt
    * @since: Jan 26, 2011
    * @version: 1.0
    * 
    * Change logs
    * Jan 26, 2011 - trungdt - add relative of pastor - subject
    */
    if (!defined("EXPONENT")) exit("");
    $pastor = null;		
    if (isset($_POST['id'])) {
        $pastor = $db->selectObject('pastors', 'id='.intval($_POST['id']));
        /*if ($album != null) {
        $loc = unserialize($album->location_data);
        }*/ 
    } else {
        /*
        // No rank in this section
        $album->rank = $db->max('album', 'rank', null, "location_data='".serialize($loc)."'");
        if ($album->rank == null) {
        $album->rank = 0;
        } else { 
        $album->rank += 1;
        }*/
    }

    //if (exponent_permissions_check("manage",$loc)) {	
    if (exponent_users_isAdmin() && exponent_users_isLoggedIn()) {
        //$config = $db->selectObject('albummodule_config',"location_data='".serialize($loc)."'");
        //  if (!$config) {
        //do nothing here yes.
        //	   $config->album_type=0; // 0 for christian
        //}

        $pastor= prayonline_subject::pastor_update($_POST, $pastor);
        //$subject->location_data = serialize($loc);
        //$album->album_type = $config->album_type;
        if (isset($pastor->id)) {
            // calculate all song length
            $db->updateObject($pastor,"pastors");
        } else {
            // save ID for later use :)
            $pastor->id=$db->insertObject($pastor,"pastors");
        }		
        // okies, now we save the relative
        $subjects=$_POST['subjects'];
        // delete old
        $db->delete("pastor_subject","pastor_id = {$pastor->id}"); 
        $temp=null;
        $temp->pastor_id = $pastor->id;
        if ($subjects != null)
        {
            foreach ($subjects as $key=>$value)
            {
                $temp->subject_id = $value;
                $db->insertObject($temp,"pastor_subject");
            }
        }
        // end 
        exponent_flow_redirect(SYS_FLOW_ACTION);
        //redirect_to($url);
    } else {
        echo SITE_403_HTML;
    }


?>
